package com.draby.starfantasymaze.activities.main.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.activities.main.adapter.AchievementAdapter;
import com.draby.starfantasymaze.dto.Achievement;
import com.draby.starfantasymaze.utils.FileUtil;

import java.util.ArrayList;
import java.util.List;

public class AchievementFragment extends Fragment {

    // 数据相关
    private String parent;
    private String achievement_fileName;
    private List<Achievement> achievements;

    // 用到的控件
    private TextView tv_ratio;
    private RecyclerView rv_achievement;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        parent = getString(R.string.file_directory);
        achievement_fileName = getString(R.string.fileName_achievement);
        return inflater.inflate(R.layout.fragment_achievement, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 找到控件
        rv_achievement = view.findViewById(R.id.rv_achievement);
        tv_ratio = view.findViewById(R.id.tv_ratio);
        // 获取成就列表
        achievements = FileUtil.getAllAchievements(parent, achievement_fileName);
        // 将成就按完成 未完成 隐藏的状态来排序
        achievements = sortAchievements(achievements);
        // 设置文本值
        setValue();
        // 设置布局与适配器
        rv_achievement.setLayoutManager(new LinearLayoutManager(getContext()));
        rv_achievement.setAdapter(new AchievementAdapter(getContext(), achievements));
    }

    /**
     * 将成就按完成 未完成 隐藏的状态来排序
     * @param achievements 排序前的成就列表
     * @return 排序后的成就列表
     */
    private List<Achievement> sortAchievements(List<Achievement> achievements) {
        List<Achievement> sortedAchievements = new ArrayList<>();
        List<Achievement> completed = new ArrayList<>();
        List<Achievement> incomplete = new ArrayList<>();
        List<Achievement> hidden = new ArrayList<>();
        // 遍历所有的成就
        for (Achievement achievement : achievements) {
            // 如果成就已完成，则添加入已完成列表
            if (achievement.getOwned()) {
                completed.add(achievement);
            } else {
                // 未完成时判断，若是隐藏成就，则添加入隐藏成就列表
                if (achievement.getHidden()) {
                    hidden.add(achievement);
                } else {
                    // 否则添加入未完成列表
                    incomplete.add(achievement);
                }
            }
        }
        sortedAchievements.addAll(completed);
        sortedAchievements.addAll(incomplete);
        sortedAchievements.addAll(hidden);
        return sortedAchievements;
    }

    private void setValue() {
        String ratio = getFinishedAchievementsCount() + " / " + achievements.size();
        tv_ratio.setText(ratio);
    }

    /**
     * 获取完成的成就的个数
     * @return 完成的成就的个数
     */
    private int getFinishedAchievementsCount() {
        int count = 0;
        for (Achievement achievement : achievements) {
            Boolean finished = achievement.getOwned();
            if (finished != null) {
                if (finished.equals(true)) {
                    count += 1;
                }
            }
        }
        return count;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        // 无论 hidden 的值，都不会执行 onCreateView 与 onViewCreated 两个方法
        if (!hidden) {
            // 重新获取成就列表
            achievements = FileUtil.getAllAchievements(parent, achievement_fileName);
            achievements = sortAchievements(achievements);
            setValue();
            rv_achievement.swapAdapter(new AchievementAdapter(getContext(), achievements), true);
        }
    }
}
